// src/store/reducers/link.reducer.js
let initialState = []

export default function (state = initialState, action) {
    switch (action.type) {
        case 'loadLinkSuccess':
            // 将新数据添加到首位
            let success = [{
                type: 'link',
                link: action.payload.link,
                logoLink: action.payload.logoLink,
                name: action.payload.name
            }, ...state]
            // 添加到缓存
            localStorage.setItem('linkList', JSON.stringify(success))
            return success
        case 'loadLinkFailed':
            // 将新数据添加到首位
            let failed = [{
                type: 'link',
                link: action.payload.link,
                logoLink: './images/failedLogo.png',
                name: action.payload.name
            }, ...state]
            // 添加到缓存
            localStorage.setItem('linkList', JSON.stringify(failed))
            return failed
        case 'loadLinkInit': 
            let init = [...action.payload]
            // 添加到缓存
            localStorage.setItem('linkList', JSON.stringify(init))
            return init
        default:
            return state
    }
}